package com.yiren.backstage.db.dao.controller;

import com.yiren.backstage.App;
import com.yiren.backstage.db.dao.DaoSession;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwb
 * @date :2021/12/20 14:13
 * @description: 数据操作控制器 user.db
 */
public class UserDbController<T> {

    public static DaoSession mDaoSession;
    public static UserDbController mUserDbController;

    /**
     * 获取单例
     */
    public static UserDbController getInstance(boolean again) {
        if (mUserDbController == null) {
            synchronized (UserDbController.class) {
                if (mUserDbController == null) {
                    mUserDbController = new UserDbController(again);
                }
            }
        }
        if (again || mDaoSession == null) {
            mDaoSession = App.getInstance().getUserSession(again);
        }
        return mUserDbController;
    }

    /**
     * 初始化
     */
    public UserDbController(boolean again) {
        mDaoSession = App.getInstance().getUserSession(again);
    }

    /**
     * 会自动判定是插入还是替换
     */
    public boolean insertOrReplace(T entity) {
        try {
            mDaoSession.insertOrReplace(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 插入一条记录，表里面要没有与之相同的记录
     */
    public boolean insert(T entity) {
        try {
            return mDaoSession.insert(entity) != -1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 更新数据  基站管理、设备配置、车辆模型、帮助中心、登录、模拟考试
     */
    public boolean update(T entity) {
        try {
            mDaoSession.update(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 更新多条记录
     */
    public boolean updateMultiple(List<T> pEntityList) {
        try {
            mDaoSession.runInTx(new Runnable() {
                @Override
                public void run() {
                    for (T entity : pEntityList) {
                        mDaoSession.update(entity);
                    }
                }
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();

        }
        return false;
    }


    /**
     * 删除单条记录
     */
    public boolean delete(T entity) {
        try {
            //按照id删除
            mDaoSession.delete(entity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除所有记录
     */
    public boolean deleteAll(Class<T> entityClass) {
        try {
            //按照id删除
            mDaoSession.deleteAll(entityClass);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据条件删除
     * @param entityClass
     * @param cond
     * @param condMore
     * @return
     */
    public boolean deleteByQueryBuilder(Class<T> entityClass,WhereCondition cond, WhereCondition... condMore) {
        try {
            mDaoSession.queryBuilder(entityClass).where(cond, condMore).buildDelete().executeDeleteWithoutDetachingEntities();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询所有记录
     */
    public List<T> queryAll(Class<T> entityClass) {
        List<T> list = new ArrayList<T>();
        try {
            list = mDaoSession.loadAll(entityClass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据主键id查询记录
     */
    public T queryById(Long key, Class<T> entityClass) {
        T t = null;
        try {
            t = mDaoSession.load(entityClass, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public T queryById(String key, Class<T> entityClass) {
        T t = null;
        try {
            t = mDaoSession.load(entityClass, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<T> queryByNativeSql(Class<T> entityClass, String sql, String[] conditions) {
        List<T> list = new ArrayList<T>();
        try {
            list = mDaoSession.queryRaw(entityClass, sql, conditions);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 使用queryBuilder进行查询
     */
    public List<T> queryByQueryBuilder(Class<T> entityClass, WhereCondition cond, WhereCondition... condMore) {
        List<T> list = new ArrayList<T>();
        try {
            QueryBuilder<T> queryBuilder = mDaoSession.queryBuilder(entityClass);
            list = queryBuilder.where(cond, condMore).list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public QueryBuilder<T> returnByQueryBuilder(Class<T> entityClass) {
        return mDaoSession.queryBuilder(entityClass);
    }
}